//
//  HomeTableViewCell.m
//  RoadMaintain
//
//  Created by 岳学坤 on 2017/2/28.
//  Copyright © 2017年 岳学坤. All rights reserved.
//

#import "HomeTableViewCell.h"
#import "TodoTableViewCell.h"
#import "MessageTableViewCell.h"
#define ToDoCellID @"cellid1"
#define MessageCellID @"cellid2"
@interface HomeTableViewCell ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong) UITableView *todoTableView;
@property (nonatomic,strong) UITableView *messageTableView;
@property (nonatomic,strong) NSMutableArray *todoArray;
@property (nonatomic,strong) NSMutableArray *messageArray;
@end
@implementation HomeTableViewCell

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        self.contentView.backgroundColor = Bai_Color;
        [self.todoTableView registerClass:[TodoTableViewCell class] forCellReuseIdentifier:ToDoCellID];
        [self.messageTableView registerClass:[MessageTableViewCell class] forCellReuseIdentifier:MessageCellID];
        [self setUpTodoTableViewHeaderView];
        [self setUpMessageTableViewHeaderView];
    }
    return self;
}
- (void)setUpTodoTableViewHeaderView{
    UIView *todoBgV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.todoTableView.frame.size.width, 30*HeightRate)];
    todoBgV.backgroundColor = MAIN_Color;
    self.todoTableView.tableHeaderView = todoBgV;
   
    UIImageView *todoImg = [[UIImageView alloc]init];
    todoImg.image = [UIImage imageNamed:@"img_waittingfor"];
    [todoBgV addSubview:todoImg];
    [todoImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(todoBgV.mas_top).offset(15*HeightRate);
        make.left.mas_equalTo(15*WidthRate);
        make.size.mas_equalTo(CGSizeMake(14*WidthRate, 14*WidthRate));
    }];
    
    UILabel *title = [[UILabel alloc]init];
    title.text = @"待办事项";
    title.textColor  = Bai_Color;
    title.font = [UIFont boldSystemFontOfSize:ZitiRate*13];
    [todoBgV addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(35*WidthRate);
        make.right.mas_equalTo(-60*WidthRate);
    }];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"更多" forState:UIControlStateNormal];
    [btn setTitleColor:Bai_Color forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:ZitiRate*13];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [btn addTarget:self action:@selector(toDoMoreClick) forControlEvents:UIControlEventTouchUpInside];
    [todoBgV addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(50*WidthRate);
        make.right.mas_equalTo(-15*WidthRate);
    }];
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = Bai_Color;
    [todoBgV addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(29*HeightRate);
        make.height.mas_equalTo(0.5);
        make.left.mas_equalTo(15*WidthRate);
        make.right.mas_equalTo(-15*WidthRate);
    }];
    
}
- (void)setUpMessageTableViewHeaderView{
    UIView *MessageBgV = [[UIView alloc]initWithFrame:CGRectMake(0, 0, self.todoTableView.frame.size.width, 30*HeightRate)];
    MessageBgV.backgroundColor = [PublicMethod colorWithHexString:@"#228dd9" andAlpha:1];
    self.messageTableView.tableHeaderView = MessageBgV;
  
    UIImageView *messageImg = [[UIImageView alloc]init];
    messageImg.image = [UIImage imageNamed:@"img_notification"];
    [MessageBgV addSubview:messageImg];
    [messageImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(MessageBgV.mas_top).offset(15*HeightRate);
        make.left.mas_equalTo(12*WidthRate);
        make.size.mas_equalTo(CGSizeMake(11*WidthRate, 11*WidthRate));
    }];
    
    UILabel *title = [[UILabel alloc]init];
    title.text = @"通知";
    title.textColor  = Bai_Color;
    title.font = [UIFont boldSystemFontOfSize:ZitiRate*13];
    [MessageBgV addSubview:title];
    [title mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.left.mas_equalTo(28*WidthRate);
        make.right.mas_equalTo(-45*WidthRate);
    }];
    UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
    [btn setTitle:@"更多" forState:UIControlStateNormal];
    [btn setTitleColor:Bai_Color forState:UIControlStateNormal];
    btn.titleLabel.font = [UIFont systemFontOfSize:ZitiRate*13];
    [btn setContentHorizontalAlignment:UIControlContentHorizontalAlignmentRight];
    [btn addTarget:self action:@selector(messageMoreClick) forControlEvents:UIControlEventTouchUpInside];
    [MessageBgV addSubview:btn];
    [btn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.width.mas_equalTo(30*WidthRate);
        make.right.mas_equalTo(-10*WidthRate);
    }];
    
    UIView *line = [[UIView alloc]init];
    line.backgroundColor = Bai_Color;
    [MessageBgV addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(29*HeightRate);
        make.height.mas_equalTo(0.5);
        make.left.mas_equalTo(10*WidthRate);
        make.right.mas_equalTo(-10*WidthRate);
    }];
}
- (void)toDoMoreClick{
  
}
- (void)messageMoreClick{

}
- (void)addValueWithTodoArray:(NSArray *)todoArray MessageArray:(NSArray *)messageArray{
    [self.todoArray removeAllObjects];
    [self.todoArray addObjectsFromArray:todoArray];
    [self.messageArray removeAllObjects];
    [self.messageArray addObjectsFromArray:messageArray];
    [self.todoTableView reloadData];
    [self.messageTableView reloadData];
}
#pragma -mark property
- (NSMutableArray *)todoArray{
    if (_todoArray == nil) {
        _todoArray = [[NSMutableArray alloc]initWithCapacity:1];
    }
    return _todoArray;
}
- (NSMutableArray *)messageArray{
    if (_messageArray==nil) {
        _messageArray = [[NSMutableArray alloc]initWithCapacity:1];
    }
    return _messageArray;
}
- (UITableView *)todoTableView{
    if (_todoTableView==nil) {
        _todoTableView  = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _todoTableView.backgroundColor = MAIN_Color;
        _todoTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _todoTableView.dataSource = self;
        _todoTableView.delegate = self;
        _todoTableView.scrollEnabled = NO;
        [self.contentView addSubview:_todoTableView];
        if (IsPad) {
        self.messageTableView.hidden = NO;
                [_todoTableView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(0);
                    make.bottom.mas_equalTo(0);
                    make.left.mas_equalTo(0);
                    make.right.equalTo(self.messageTableView.mas_left).offset(0);
                }];
           
            
        }else{
            self.messageTableView.hidden = YES;
            [_todoTableView mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.mas_equalTo(0);
                make.bottom.mas_equalTo(0);
                make.left.mas_equalTo(0);
                make.right.mas_equalTo(0);
            }];
            
        }

    }
    return _todoTableView;
}
- (UITableView *)messageTableView{
    if (_messageTableView==nil) {
        _messageTableView  = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _messageTableView.backgroundColor = [PublicMethod colorWithHexString:@"#228dd9" andAlpha:1];
        _messageTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _messageTableView.dataSource = self;
        _messageTableView.delegate = self;
        _messageTableView.scrollEnabled = NO;
        [self.contentView addSubview:_messageTableView];
        if (IsPad) {
            
                [_messageTableView mas_makeConstraints:^(MASConstraintMaker *make) {
                    make.top.mas_equalTo(0);
                    make.bottom.mas_equalTo(0);
                    make.right.mas_equalTo(0);
                    make.width.mas_equalTo(125*WidthRate);
                }];
             _messageTableView.hidden = NO;
        }else{
        [_messageTableView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.mas_equalTo(0);
            make.bottom.mas_equalTo(0);
            make.left.mas_equalTo(0);
            make.right.mas_equalTo(0);
        }];
            _messageTableView.hidden = YES;
        }
    }
    return _messageTableView;
}
#pragma -mark delegate

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 30*HeightRate;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if ([tableView isEqual:self.todoTableView]) {
        return self.todoArray.count;
    }else{
        return self.messageArray.count;
    }
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ([tableView isEqual:self.todoTableView]) {
        TodoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:ToDoCellID];
        [cell addValue:self.todoArray[indexPath.row]];
        return cell;
    }else{
        MessageTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:MessageCellID];
        if (indexPath.row == self.messageArray.count-1) {
            cell.line.hidden = YES;
        }else{
            cell.line.hidden = NO;
        }
        return cell;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{

}
@end
